package com.say.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.say.bean.domain.User;
import com.say.utils.UserUtil;

@Component
public class AuthInterceptor extends HandlerInterceptorAdapter {
	 
	// Controller 前执行
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        response.setCharacterEncoding("UTF8"); // 设置输出编码
        response.setContentType("text/html;charset=UTF-8");
//      String ua = ((HttpServletRequest) request).getHeader("user-agent").toLowerCase();  
//		if (ua.indexOf("micromessenger") > 0) {
//			//是微信浏览器，进入微信授权 
//			boolean bool= UserUtil.WeixinOauth2(request,response);//是否从微信浏览器分享进来
//		    if(!bool){
//		     	return false;
//		    }
//		} else{
			//不是微信浏览器，进入login 
	        User user = UserUtil.getUser(request);
			if(user==null) {
				response.sendRedirect("/login.html");
				return false;
			}
//		}
        return true;
    }
    
    //运行Controller后运行
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
       
    }

    

}
